#pragma once

#include <QTcpServer>
#include <QTcpSocket>
#include <QJsonObject>
#include <functional>

namespace infrastructure {
namespace io {

/**
 * @brief 简单的HTTP服务器
 * @details 用于接收第三方系统的HTTP POST请求
 */
class HttpServer : public QTcpServer {
    Q_OBJECT

public:
    explicit HttpServer(QObject* parent = nullptr);
    ~HttpServer() = default;

    /**
     * @brief 响应POST请求
     * @param socket TCP套接字
     * @param jsonObj 响应的JSON对象
     */
    void respondPost(QTcpSocket* socket, const QJsonObject& jsonObj);

    /**
     * @brief 设置数据接收回调
     */
    using receive_callback_t = std::function<void(QTcpSocket*, QString)>;
    receive_callback_t receive_callback = nullptr;

protected:
    void incomingConnection(qintptr socketDescriptor) override;

private slots:
    void socketReadyRead();
    void socketDisconnected();

private:
    void processPostData(const QString& postData);
};

} // namespace io
} // namespace infrastructure
